package com.example.front.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.front.entity.Equipment;
import com.example.front.serivce.EquipmentService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "设备台账")
@CrossOrigin
@RestController
@RequestMapping("/front/equipment")
public class EquipmentController {
    @Resource
    EquipmentService equipmentService;

    @ApiOperation("添加设备")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid Equipment equipment) {
        User user= UserContext.getUser();
        equipment.setTenantId(user.getTenantId());
        equipmentService.createEquipment(equipment);
        return new JsonResult<>(true, "添加成功");
    }

    @ApiOperation("列出设备列表")
    @GetMapping("/list")
    public JsonResult<ListDto<Equipment>> list(@Valid Equipment condition, Pagination pagination) {
        User user= UserContext.getUser();
        condition.setTenantId(UserContext.getUser().getTenantId());
        ListDto<Equipment> equipmentList = equipmentService.listEquipment(condition, pagination);
        return new JsonResult<>(equipmentList);
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid Equipment equipment) {
        equipmentService.update(equipment);
        return new JsonResult<>();
    }

    @ApiOperation("删除设备")
    @PostMapping("/delete")
    public JsonResult<String> delete(long id) {
        equipmentService.deleteById(id,UserContext.getUser().getTenantId());
        return new JsonResult<>();
    }
}
